package com.vincce.sport.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.core.utils.SimpleStorageClient;
import com.vincce.sport.entity.PreliminaryContest;
import com.vincce.sport.entity.ShootSpot;
import com.vincce.sport.entity.Team;
import com.vincce.sport.mapper.PreliminaryContestMapper;
import com.vincce.sport.mapper.ShootSpotMapper;
import com.vincce.sport.mapper.TeamDateMapper;
import com.vincce.sport.mapper.TeamMapper;

@Controller
@RequestMapping("/spread")
public class SpreadController {

	@Resource
	private TeamMapper teamMapper;
	@Resource
	private PreliminaryContestMapper preliminaryContestMapper;
	@Resource
	private TeamDateMapper teamDateMapper;
	@Resource
	private ShootSpotMapper shootSpotMapper;
	
	@RequestMapping("/show")
	public String show(HttpServletRequest request,Model model,
			PreliminaryContest preliminaryContest){
		Map<String, Object> filter = new HashMap<String, Object>();
		if(preliminaryContest.getId() != null && !preliminaryContest.getId().equals("")){
            filter.put("preliminaryContestId", preliminaryContest.getId());
		}
		//获取双方球队基本信息
		Map<String,Object> teamMap = preliminaryContestMapper.findTeamByContestId(preliminaryContest.getId());
		String team_one_id = teamMap.get("team_id").toString();
		String team_two_id = teamMap.get("team_id_two").toString();
		//获取球队信息
		Team team1 = teamMapper.getTeamAndSchoolById(team_one_id);
		Team team2 = teamMapper.getTeamAndSchoolById(team_two_id);
		//获取比分
		Map point1 = new HashMap();
		Map point2 = new HashMap();
		point1.put("team_id", team_one_id);
		point1.put("contest_id", preliminaryContest.getId());
		point2.put("team_id", team_two_id);
		point2.put("contest_id", preliminaryContest.getId());
		team1.setTotalPoint(teamDateMapper.getTotalPoint(point1));
		team1.setLogUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+team1.getLogUrl(),"ori"));
		team2.setTotalPoint(teamDateMapper.getTotalPoint(point2));
		team2.setLogUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+team2.getLogUrl(),"ori"));
		model.addAttribute("team1", team1);
		model.addAttribute("team2", team2);
		//获取分布点
		ShootSpot shootSpot1 = shootSpotMapper.getshootSpotByTeamAndContest(point1);
		ShootSpot shootSpot2 = shootSpotMapper.getshootSpotByTeamAndContest(point2);
		shootSpot1 = shootSpot1 == null?new ShootSpot() : shootSpot1;
		shootSpot2 = shootSpot2 == null?new ShootSpot() : shootSpot2;
		model.addAttribute("shootSpot1", replace(shootSpot1));
		model.addAttribute("shootSpot2", replace(shootSpot2));
		return "foreground/spread";
	}
	
	//转换图片路径
		private ShootSpot replace(ShootSpot itl) {
					itl.setFullTimeUrl(CheckUtil.isEmpty(itl.getFullTimeUrl()) ? "" :String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getFullTimeUrl(), "ori"));
					itl.setFirstTimeUrl(CheckUtil.isEmpty(itl.getFirstTimeUrl()) ? "" :String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getFirstTimeUrl(), "ori"));
					itl.setSecondTimeUrl(CheckUtil.isEmpty(itl.getSecondTimeUrl()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getSecondTimeUrl(), "ori"));
					itl.setThirdTimeUrl(CheckUtil.isEmpty(itl.getThirdTimeUrl()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getThirdTimeUrl(), "ori"));
					itl.setFourthTimeUrl(CheckUtil.isEmpty(itl.getFourthTimeUrl()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getFourthTimeUrl(), "ori"));
					itl.setExtraTimrUrl(CheckUtil.isEmpty(itl.getExtraTimrUrl()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getExtraTimrUrl(), "ori"));
					itl.setExtraTimrUrl2(CheckUtil.isEmpty(itl.getExtraTimrUrl2()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getExtraTimrUrl2(), "ori"));
					itl.setExtraTimrUrl3(CheckUtil.isEmpty(itl.getExtraTimrUrl3()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getExtraTimrUrl3(), "ori"));
					itl.setExtraTimrUrl4(CheckUtil.isEmpty(itl.getExtraTimrUrl4()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getExtraTimrUrl4(), "ori"));
			return itl;
		}
}
